<?php 

class order  {



	public function add($parm){
		try {
			$result = DB::select('*')->from('sysorder')->where("userId","=",$parm['userId'])->and_where("uuid","=",$parm['uuid'])->execute()->as_array();
			if(count($result)!= 0){
				return $result[0]['orderId'];
			}

			Database::instance()->begin();
			
			DB::insert("sysorder",array('orderId','money','status','addressId','userId','expressId','uuid'))
				->values(array($parm['orderId'],$parm['money'],$parm['status'],$parm['addressId'],$parm['userId'],$parm['expressId'],$parm['uuid']))
				->execute();
			foreach($parm['productId'] as $key => $value){
				DB::query(Database::UPDATE,
	        	 'UPDATE sysproductexpresssku  set  freeze = freeze+'.$parm['number'][$key].' where  specificationId= '.$parm['specificationId'][$key])->execute();
				DB::insert("sysorderdetail",array('orderId','productId','number','specificationId'))
					->values(array($parm['orderId'],$value,$parm['number'][$key],$parm['specificationId'][$key]))
					->execute();
			}

			Database::instance()->commit();
			return $parm['orderId'];
		}catch (Exception $e){
			$message['data'] = $e->getMessage();
			$message['msg']  =false;
			echo '<script>alert("'.$e->getMessage().'")</script>';
			Database::instance()->rollback();
		}
		return $message;
	}
	public function getByOrderId($parm){
		$result = DB::select('sysorder.addressId,sysorder.status as orderstatus,sysproductexpresssku.price,sysspecification.*,sysorderdetail.specificationId,sysorderdetail.productId,sysorder.* ,sysorderdetail.number,sysproduct.* ,sysproductdesc.mainpic,sysuseraddress.*')
			->from('sysorder')->join('sysorderdetail')->on("sysorderdetail.orderId","=","sysorder.orderId")
			->join("sysproductdesc")

			->on('sysorderdetail.productId',"=","sysproductdesc.productId")
			->join("sysproduct")
			->on("sysproduct.id","=","sysorderdetail.productId")
			->join('sysuseraddress')
			->on("sysuseraddress.id","=","sysorder.addressId")
			->join('sysspecification')
			->on("sysspecification.id","=","sysorderdetail.specificationId")
			->join('sysproductexpresssku')
			->on("sysspecification.id","=","sysproductexpresssku.specificationId")
			->where("sysorder.userId","=",$parm['userId'])->and_where("sysorder.orderId","=",$parm['orderId'])->execute()->as_array();

		return $result;
	}
	public function updateMoneyByOrderId($parm){

		DB::update("sysorder")->set(array('money'=>$parm['money']))->where('orderId','=',$parm['orderId'])->execute();
	}

	public function updateExpress($parm){
		$result = DB::select('*')->from('sysorder')->where("userId","=",$parm['userId'])->and_where("orderId","=",$parm['orderId'])->execute()->as_array();
		if(count($result) == 0 ||$result[0]['status']='订单已支付'||$result[0]['status']=='订单已完成'){
			return ;
		}
		DB::update("sysorder")->set(array('expressId'=>$parm['expressId']))->where('orderId','=',$parm['orderId'])->execute();
	}
	public function updateNumber($parm){
		try {
			$result = DB::select('*')->from('sysorder')->where("userId","=",$parm['userId'])->and_where("orderId","=",$parm['orderId'])->execute()->as_array();
			if(count($result) == 0 ||$result[0]['status']=='订单已支付'||$result[0]['status']=='订单已完成'){
				return ;
			}
			Database::instance()->begin();
			$parm['product'] = explode(',', $parm['product']);
			$parm['number'] = explode(',', $parm['number']);
			foreach($parm['product'] as $key => $value){
				$result = DB::select('*')->from('sysorderdetail')->where('orderId','=',$parm['orderId'])->and_where('productId','=',$parm['product'][$key])->execute();
				$freeze = $parm['number'][$key]-$result[0]['number'];
				DB::query(Database::UPDATE,
					'UPDATE sysproductexpresssku  set  freeze = freeze+'.$freeze.' where  specificationId= '.$result[0]['specificationId'])->execute();
				DB::update("sysorderdetail")->set(array('number'=>$parm['number'][$key]))->where('orderId','=',$parm['orderId'])->and_where('productId','=',$parm['product'][$key])->execute();
			}

			Database::instance()->commit();
		}catch (Exception $e){
				Database::instance()->rollback();
		}
	}
	public function updateAddress($parm){
		$result = DB::select('*')->from('sysorder')->where("userId","=",$parm['userId'])->and_where("orderId","=",$parm['orderId'])->execute()->as_array();
		if(count($result) == 0 ||$result[0]['status']=='订单已支付'||$result[0]['status']=='订单已完成'){
			return ;
		}
		DB::update("sysorder")->set(array('addressId'=>$parm['addressId']))->where('orderId','=',$parm['orderId'])->execute();
	}
}